/**
* Created by chenly on 12/29/23.
* Description
**/


#ifndef SIMNOW_TEST_MDDATAQUEUE_H
#define SIMNOW_TEST_MDDATAQUEUE_H

#include <list>

using namespace std;

class MdDataQueue {
    list<CThostFtdcDepthMarketDataField> m_queue;
    mutex m_mtx;
    condition_variable m_cv;
    bool m_ready = false;
    bool m_exit = false;
public:
    void set_exit(bool exit) {
        m_exit = exit;
    }
    bool exit(){
        return m_exit;
    }
    void next(CThostFtdcDepthMarketDataField &data) {
        std::unique_lock lk(m_mtx);
        m_cv.wait(lk, [&] { return m_ready; });
        data = m_queue.back();
        m_queue.pop_back();
        m_ready = false;
        lk.unlock();
    }
    void add_data(CThostFtdcDepthMarketDataField &data) {
        lock_guard lk(m_mtx);
        m_queue.push_front(data);
        m_ready = true;
        m_cv.notify_one();
    }
};

#endif //SIMNOW_TEST_MDDATAQUEUE_H
